﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.DAL;
using MyOA.Model;

namespace MyOA.BLL
{
    public class DistributeRoleManage
    {
        DistributeRoleService service = new DistributeRoleService();
        IList<SysFun> sysAllFunList = null;
        IList<SysFun> sysFunListByRoleName = null;
        public DistributeRoleManage(string roleName)
        {
            sysAllFunList = getAllSysFun();
            sysFunListByRoleName = getSysFunByRoleName(roleName);
        }
        public DistributeRoleManage() { }
        /// <summary>
        /// 获取全部的菜单信息
        /// </summary>
        /// <returns>返回全部的菜单信息</returns>
        private IList<SysFun> getAllSysFun()
        {
            return service.getAllSysFun();
        }

        /// <summary>
        /// 获取主菜单
        /// </summary>
        /// <param name="loginId">登录号</param>
        /// <returns>返回主菜单名</returns>
        public IList<SysFun> getMainMenu()
        {


            //删选ParentNodeId为0的集合
            var mainMenus = from sysFun in sysAllFunList
                            where sysFun.ParentNodeId == 0
                            select sysFun;

            return mainMenus.ToList<SysFun>();
        }

        /// <summary>
        /// 根据主菜单ID获取子菜单集合
        /// </summary>
        /// <param name="mainMenuId">主菜单ID</param>
        /// <returns>返回子菜单集合</returns>
        public IList<SysFun> getChildrenMenusByMainMenuId(int mainMenuId)
        {
            var childrenMenus = from sysFun in sysAllFunList
                                where sysFun.ParentNodeId == mainMenuId
                                select sysFun;
            return childrenMenus.ToList<SysFun>();
        }



        //*************根据角色名获取此登陆名的权限**********************//
        /// <summary>
        /// 根据角色名获取权限信息集合
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <returns>返回权限信息</returns>      
        private IList<SysFun> getSysFunByRoleName(string roleName)
        {
            return service.getSysFunByRoleName(roleName);
        }

        /// <summary>
        /// 根据角色名获取主菜单
        /// </summary>
        /// <param name="roleName">角色名</param>
        /// <returns>返回主菜单名</returns>
        public IList<SysFun> getMainMenuByRoleName()
        {


            //删选ParentNodeId为0的集合
            var mainMenus = from sysFun in sysFunListByRoleName
                            where sysFun.ParentNodeId == 0
                            select sysFun;

            return mainMenus.ToList<SysFun>();
        }

        /// <summary>
        /// 根据角色名，主菜单ID获取子菜单集合
        /// </summary>
        /// <param name="mainMenuId">主菜单ID</param>
        /// <returns>返回子菜单集合</returns>
        public IList<SysFun> getChildrenMenusByRoleNameAndMainMenuId(int mainMenuId)
        {
            var childrenMenus = from sysFun in sysFunListByRoleName
                                where sysFun.ParentNodeId == mainMenuId
                                select sysFun;
            return childrenMenus.ToList<SysFun>();
        }
        //*******************************************//

        /// <summary>
        /// 根据角色ID来获取此角色拥有的权限ID集合
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回此角色拥有的权限ID集合</returns>
        public IList<RoleRight> getRoleRightByRoleId(int roleId)
        {
            return service.getRoleRightByRoleId(roleId);
        }

        /// <summary>
        /// 增加或删除指定角色的权限
        /// </summary>
        /// <param name="roleId">角色ID</param>    
        /// <param name="deleteNodeId">删除的权限ID字符串</param>
        /// <param name="insertNodeId">增加的权限ID字符串</param>
        /// <returns>是否增加或删除成功</returns>
        public bool insertAndRoleRight(int roleId, string deleteNodeId, string insertNodeId)
        {
            return service.insertAndRoleRight(roleId, deleteNodeId, insertNodeId);
        }
    }
}
